﻿using System;
using System.Globalization;
using System.Web.UI;
using AB.ServiceDirectory.BO;
using AB.ServiceDirectory.Entities;

namespace AB.ServiceDirectory.Design.Org
{
    public partial class HEditSupportingMaterial : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDropDownListType();
                BindFields();
            }
            VisibleDeactiveButton();
        }

        /// <summary>
        /// Buttons the save click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonSaveClick(object sender, EventArgs e)
        {
            bool result=false;
            var supportingMaterial = GetSupportingMaterial();
            if (supportingMaterial!=null)
            {
                result = HSupportingMaterialBO.UpdateSupportingMaterial(supportingMaterial); 
            }
            if (result)
            {
                Response.Redirect("~/Organisation/HSupportingMaterials.aspx");
            }
        }

        /// <summary>
        /// Binds the fields.
        /// </summary>
        private void BindFields()
        {
            int id = 0;
            if (!int.TryParse(Request.Params["id"], out id))
            {
                return;
            }

            SupportingMaterial suppportingMaterial = HSupportingMaterialBO.GetSupportingMaterialById(id);
            TextBoxURL.Text = suppportingMaterial.Url;
            TextBoxDescription.Text = suppportingMaterial.Description;
            TextBoxAddedBy.Text = suppportingMaterial.User.Account;
            LabelIsActive.Text = (bool) suppportingMaterial.IsActive ? "Yes" : "No";
            TextBoxAddedDate.Text = suppportingMaterial.AddedDate.ToString(CultureInfo.CurrentCulture);
            DropDownListType.SelectedValue = suppportingMaterial.Type;
            HiddenFieldUserId.Value = suppportingMaterial.UserId.ToString(CultureInfo.CurrentCulture);
            HiddenFieldSupportingMaterialId.Value = id.ToString(CultureInfo.CurrentCulture);
        }

        /// <summary>
        /// Gets the supporting material from fields in the Page
        /// </summary>
        private SupportingMaterial GetSupportingMaterial()
        {
            var supportingMaterial = new SupportingMaterial();
            supportingMaterial.SupportingMaterialId = Convert.ToInt32(HiddenFieldSupportingMaterialId.Value,CultureInfo.CurrentCulture);
            supportingMaterial.Url = string.IsNullOrEmpty(TextBoxURL.Text) ? string.Empty : TextBoxURL.Text;
            supportingMaterial.Description = string.IsNullOrEmpty(TextBoxDescription.Text)
                                                 ? string.Empty
                                                 : TextBoxDescription.Text;
            supportingMaterial.Type = string.IsNullOrEmpty(DropDownListType.SelectedValue)
                                          ? string.Empty
                                          : DropDownListType.SelectedValue;
            supportingMaterial.UserId = int.Parse(HiddenFieldUserId.Value);
            supportingMaterial.AddedDate = DateTime.Now;
            supportingMaterial.IsActive = LabelIsActive.Text.Equals("Yes") ? true : false;
            supportingMaterial.OrganisationId = 1; //organisation name=Celeste662
            return supportingMaterial;
        }

        /// <summary>
        /// Binds the type of the drop down list.
        /// </summary>
        private void BindDropDownListType()
        {
            DropDownListType.DataSource = HSupportingMaterialBO.GetAllSupportingMaterialType();
            DropDownListType.DataBind();
        }

        /// <summary>
        /// Buttons the deactive click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonInactiveClick(object sender, EventArgs e)
        {
            LabelIsActive.Text = "No";
        }

        /// <summary>
        /// Visibles the deactive button.
        /// </summary>
        private void VisibleDeactiveButton()
        {
            ButtonInactive.Visible = LabelIsActive.Text.Equals("Yes") ? true : false;
        }
    }
}